package com.group.view;

import com.group.controller.StudentController;
import com.group.model.Student;

import javax.swing.*;
import java.awt.*;

/**
 * @author lenovo
 */
public class AddScorePanel extends JPanel {
    JTextField idField = new JTextField();
    JTextField courseField = new JTextField();
    JTextField scoreField = new JTextField();
    JLabel status = new JLabel();

    public AddScorePanel() {
        setLayout(new GridLayout(4, 2));
        add(new JLabel("学号:")); add(idField);
        add(new JLabel("课程名（Math/Java/PE）:")); add(courseField);
        add(new JLabel("成绩:")); add(scoreField);
        JButton addBtn = new JButton("添加成绩");
        add(addBtn); add(status);

        addBtn.addActionListener(e -> {
            String id = idField.getText().trim();
            String course = courseField.getText().trim();
            String scoreText = scoreField.getText().trim();
            try {
                double score = Double.parseDouble(scoreText);
                Student s = StudentController.get(id);
                if (s != null) {
                    s.addScore(course, score);
                    try {
                        StudentController.saveToFile("saveStudents.txt");
                    } catch (Exception ex) {
                        //
                    }
                    status.setText("成绩添加成功");
                } else {
                    status.setText("学生不存在");
                }
            } catch (NumberFormatException ex) {
                status.setText("成绩必须为数字");
            }
        });
    }
}
